﻿using MDK.Common;
using MDK.Device.Devices;
using MDK.Model.Models;
using Newtonsoft.Json;
using System;
using System.Windows.Forms;

namespace MDK.Winform
{
    public partial class frmCentrifugeConfig : BaseForm
    {
        private T_CentrifugeSetting _set { get; set; }

        public frmCentrifugeConfig()
        {
            InitializeComponent();
        }

        private void frmCentrifugeConfig_Load(object sender, EventArgs e)
        {
            BingData();
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BingData()
        {
            string url = string.Format("CentrifugeSetting/Find?jsonWhere={0}", "");
            _set = WebAPIHelper.Get<T_CentrifugeSetting>(url);
            if (_set == null)
                return;
            txtTime.Text = _set.CentrifugeTime.ToString();
            txtTemp.Text = _set.CentrifugeTemperature;
            txtSpeed.Text = _set.CentrifugeSpeed.ToString();
            txtNoBalance.Text = _set.CentrifugeNoBalance;
        }


        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_OK_Click(object sender, EventArgs e)
        {
            try
            {
                int time = 0; int.TryParse(txtTime.Text.Trim(), out time);
                double speed = 0; double.TryParse(txtSpeed.Text.Trim(), out speed);
                if (string.IsNullOrEmpty(_set.Id))
                {
                    _set = new T_CentrifugeSetting();
                    _set.Id = Guid.NewGuid().ToString("N");
                    _set.CentrifugeTime = time;
                    _set.CentrifugeTemperature = txtTemp.Text.Trim();
                    _set.CentrifugeSpeed = speed;
                    _set.CentrifugeNoBalance = txtNoBalance.Text.Trim();
                    WebAPIHelper.Post<T_CentrifugeSetting>("CentrifugeSetting/Add", _set);
                }
                else
                {
                    _set.CentrifugeTime = time;
                    _set.CentrifugeTemperature = txtTemp.Text.Trim();
                    _set.CentrifugeSpeed = speed;
                    _set.CentrifugeNoBalance = txtNoBalance.Text.Trim();
                    WebAPIHelper.Post<T_CentrifugeSetting>("CentrifugeSetting/Modify", _set);
                }

                WriteToMachine();
                FastMessageBox.Show("编辑成功");
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                FastMessageBox.Error(ex.Message);
            }
        }
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
            DialogResult = DialogResult.Cancel;
        }

        public void WriteToMachine()
        {
            try
            {
                Centrifuge leftcentrifuge = App.flow.SysModel.Centrifuges[0];
                Centrifuge rightcentrifuge = App.flow.SysModel.Centrifuges[1];

                int speed = 0; int.TryParse(txtSpeed.Text.Trim(), out speed);
                leftcentrifuge.Speed = speed;
                rightcentrifuge.Speed = speed;
                leftcentrifuge.RunTime = _set.CentrifugeTime.Value;
                int Balance = 0; int.TryParse(_set.CentrifugeNoBalance, out Balance);
                leftcentrifuge.Balance = Balance;
                rightcentrifuge.Balance = Balance;
                double Temperature = 0; double.TryParse(_set.CentrifugeTemperature, out Temperature);
                leftcentrifuge.Temperature = Temperature;
                rightcentrifuge.Temperature = Temperature;
            }
            catch
            {
                FastMessageBox.Error("设置失败,离心机未连接");
            }
        }

       
    }
}
